package com.fudan.cfg.trans;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.fudan.cfg.base.TransDef;
import com.fudan.run.ctx.annotation.TransAdapter;

@TransAdapter("combine")
public class CombineNode extends TransDef{

	private static final String TARGET = "target";
	private static final String PARTITION = "partition";

	private List<TargetNode> targetNodeList;
	private int partition = 2;
	private String keyAs;

	public CombineNode(String name, String type,String target) {
		super(name, type,target);
	}

	public List<TargetNode> getTargetNodeList() {
		return targetNodeList;
	}
	public void setTargetNodeList(List<TargetNode> targetNodeList) {
		this.targetNodeList = targetNodeList;
	}
	public int getPartition() {
		return partition;
	}
	public void setPartition(int partition) {
		this.partition = partition;
	}
	@Override
	public TransDef build(Map<String, Object> configuration) {
		this.keyAs = (String) configuration.get("keyAs");
		List<Map<String,Object>> targetNodes = (List<Map<String, Object>>) configuration.get(TARGET);
		if(targetNodes == null) {
			throw new RuntimeException(String.format("combine 节点没有设置target, name:%s,type:%s",name,type));
		}
		targetNodeList = new ArrayList<>();
		for (Map<String, Object> map : targetNodes) {
			TargetNode targetNode = TargetNode.build(map);
			targetNodeList.add(targetNode);
		}
		Object partition = configuration.get(PARTITION);
		if(partition != null) {
			try {
				this.partition = (Integer)partition;
			}catch(Exception e) {
				throw new RuntimeException(String.format("combine节点解析partition失败，name:%s,type:%s", name,type));
			}
		}
		return this;
	}
	public String getKeyAs() {
		return keyAs;
	}
	public void setKeyAs(String keyAs) {
		this.keyAs = keyAs;
	}

}
